package model.impl;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ServiceDispatcher
{
	private static ServiceDispatcher instance;
	ExecutorService executor = null;
	

	private boolean stop;
	static final long timeKeepAliveMS = 3000; 
	
	public static ServiceDispatcher getInstance()
	{
		if( instance == null )
		{
			instance = new ServiceDispatcher();
		}
		return instance;
	}
	
	public void init()
	{
		
	}
	
	public void destroy()
	{
		this.executor.shutdown();
		
		while(!executor.isTerminated())
		{
			
		}
	}
	
	protected ServiceDispatcher()
	{
		executor = Executors.newSingleThreadExecutor();
	}
	
	public synchronized void enqueue( final Task t )
	{
		executor.submit(new Runnable()
		{
			
			@Override
			public void run()
			{
				// TODO Auto-generated method stub
				System.out.println("Run()...");
				t.execute();			
				System.out.println("Run done!");
			}
		});
	}
}
